package com.zhongxiangspace.controller.pagemanage;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.zhongxiangspace.entity.ArticleType;
import com.zhongxiangspace.entity.Articles;
import com.zhongxiangspace.entity.QueryAnalyzer;
import com.zhongxiangspace.entity.StationBranch;
import com.zhongxiangspace.result.ExceptionResult;
import com.zhongxiangspace.result.NormalResult;
import com.zhongxiangspace.result.Response;
import com.zhongxiangspace.result.Result;
import com.zhongxiangspace.service.ArticleService;
import com.zhongxiangspace.tools.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 文章
 */
@Controller
public class ArticleController {

    @Autowired
    private ArticleService articleService;


    /**
     * 逻辑删除文章
     */
    @ResponseBody
    @PostMapping("/article/details/updataDetails.do")
    public Result updataDetails(Articles articles){
        try {
            articles.setModify_time(DateUtil.getDateTime());
          boolean flag=this.articleService.updataDetails(articles);
            return  new NormalResult(true);
        }catch (Exception e){
            return  new ExceptionResult(e);
        }
    }

    @RequestMapping("/article/details/edit.do")
    public String edit(Model model,Integer id) {
        List<ArticleType> articleTypes = this.articleService.queryType(1);
        model.addAttribute("articleTypes", articleTypes);
        Articles articles = this.articleService.queryOne(id);
        model.addAttribute("articles", articles);
        String gson = new Gson().toJson(articles);
        model.addAttribute("gson", gson);
        return "basics/article/article_updata";
    }
    /**
     *文章预览
     *
     * @return
     */
    @RequestMapping("/article/details/details-see.do")
    public String details_see(Model model,Integer id) {
        Articles articles = this.articleService.queryOne(id);
        model.addAttribute("articles", articles);
        return "basics/article/article_detail";
    }

    /**
     * 逻辑删除文章
     */
    @ResponseBody
    @PostMapping("/article/details/delete.do")
    public Result deleteDetails(Integer id){
        try {
            boolean flag=this.articleService.deleteDetails(id);
            return  new NormalResult(flag);
        }catch (Exception e){
            return  new ExceptionResult(e);
        }
    }


    /**
     * 异步热门和非热门
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/article/details/statusHot.do")
    public Result statusHot(Integer id) {
        boolean flag=false;
        try {
             Articles articles = this.articleService.queryOne(id);
             articles.setModify_time(DateUtil.getDateTime());
            if (articles != null && articles.getHotCode().equals(0)) {
                articles.setHotCode(1);
                 flag = this.articleService.updataStateHot(articles);
            } else if (articles != null && articles.getHotCode().equals(1)) {
                articles.setHotCode(0);
                 flag = this.articleService.updataStateHot(articles);
            }
            return new NormalResult(flag);
        } catch (Exception e) {
            e.printStackTrace();
            return new ExceptionResult(e);
        }
    }


    /**
     * 异步修改上下线 data
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/article/details/status.do")
    public Result statusArticle(Integer id) {
        System.out.println(id);
        boolean flag=false;
        try {
            Articles articles = this.articleService.queryOne(id);
            articles.setModify_time(DateUtil.getDateTime());
            if (articles != null && articles.getShowCode().equals(0)) {
                articles.setShowCode(1);
                 flag = this.articleService.updataStates(articles);
            } else if (articles != null && articles.getShowCode().equals(1)) {
                articles.setShowCode(0);
                 flag = this.articleService.updataStates(articles);
            }
            return new NormalResult(flag);
        } catch (Exception e) {
            return new ExceptionResult(e);
        }
    }

    /**
     *文章列表
     *
     * @return
     */
    @RequestMapping("/article/details/list.do")
    public String details(Model model) {
        List<ArticleType> articleTypes = this.articleService.queryType(1);
        model.addAttribute("articleTypes", articleTypes);
        return "basics/article/article_list";
    }

    /**
     * 列表内容
     * @return
     */
    @ResponseBody
    @GetMapping("/article/details/ajaxList.do")
    public Response ajaxList(QueryAnalyzer queryAnalyzer) {
        PageHelper.startPage(queryAnalyzer.getPage(), queryAnalyzer.getLimit(), "articles.modify_time  DESC");
        try {
            Page<Map<String,Object>> articles = (Page<Map<String,Object>>) this.articleService.articlesAll(queryAnalyzer);
            return new Response(0, "SUCCESS", articles.getTotal(), articles.getResult());
        }catch (Exception e){
            return new Response(0, "SUCCESS", 0,e);
        }
    }


    /**
     * 文章详情添加
     *
     * @return
     */
    @RequestMapping("/article/details/add.do")
    public String addArticle(Model model) {
        List<ArticleType> articleTypes = this.articleService.queryType(1);
        model.addAttribute("articleTypes", articleTypes);
        return "basics/article/article_add";
    }


    /**
     * 添加房源
     *
     * @param articles
     * @return
     */
    @ResponseBody
    @PostMapping("/article/details/insertArticles.do")
    public Result insertArticles(Articles articles) {
        try {
            articles.setCreate_time(DateUtil.getDateTime());
            articles.setModify_time(DateUtil.getDateTime());
            boolean flag=this.articleService.insertArticles(articles);
            return new  NormalResult(flag);
        } catch (Exception e) {
            return new ExceptionResult(e);
        }
    }


    /**
     * 异步删除 data
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/article/type/delete.do")
    public Response delete(Integer id) {
        boolean flag = false;
        if (id != null) {
            flag = this.articleService.delete(id);
        }
        Response result = new Response(0, "success", 0, flag);
        return result;
    }

    /**
     * 异步修改上下线 data
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/article/type/status.do")
    public Response status(Integer id) {
        boolean flag = false;
        if (id != null) {
            ArticleType articleType = this.articleService.queryTypeOne(id);
            articleType.setModify_time(DateUtil.getDateTime());
            if (articleType != null && articleType.getShowCode().equals(0)) {
                articleType.setShowCode(1);
                flag = this.articleService.updataStatusCode(articleType);
            } else if (articleType != null && articleType.getShowCode().equals(1)) {
                articleType.setShowCode(0);
                flag = this.articleService.updataStatusCode(articleType);
            }
        }
        Response result = new Response(0, "", 0, flag);
        return result;
    }

    /**
     * 分类
     *
     * @return
     */
    @GetMapping("/article/type/show.do")
    public String show() {
        return "basics/article/article_type";
    }

    /**
     * 分类
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/article/type/list.do")
    public Response articleTypeList(QueryAnalyzer queryAnalyzer) {
        try {
            PageHelper.startPage(queryAnalyzer.getPage(), queryAnalyzer.getLimit(), "modify_time  DESC");
            Integer state = null;
            Page<ArticleType> articleTypes = (Page<ArticleType>) this.articleService.queryType(state);
            return new Response(0, "SUCCESS", articleTypes.getTotal(), articleTypes.getResult());
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(1, e.getMessage(), 0, e);
        }
    }

    /**
     * 分类
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/article/type/addByUpdata.do")
    public Result addByUpdata(ArticleType articleType) {
        try {
            boolean flag = false;
            articleType.setModify_time(DateUtil.getDateTime());
            if (articleType.getId() == null) {
                articleType.setCreate_time(DateUtil.getDateTime());
                flag = this.articleService.articleTypeAdd(articleType);
            } else {
                flag = this.articleService.updataType(articleType);
            }
            return new NormalResult(flag);
        } catch (Exception e) {
            return new ExceptionResult(e);
        }
    }
}
